extends Control
class_name PlayerStats

# 玩家统计信息显示组件
# 显示等级、经验值、金币等数据

@export var player_level: int = 1
@export var current_experience: int = 0
@export var experience_to_next_level: int = 100
@export var player_coins: int = 0

# UI节点
var stats_panel: Panel
var level_label: Label
var exp_label: Label
var exp_bar: ProgressBar
var coins_label: Label
var coins_icon: TextureRect

# 初始化标志
var is_initialized: bool = false

# 面板配置 - 调整为更适合小屏幕的尺寸
const PANEL_WIDTH = 130  # 稍微缩小
const PANEL_HEIGHT = 50  # 稍微缩小

func _ready():
	if not is_initialized:
		initialize()

func initialize():
	if is_initialized:
		print("PlayerStats 已经初始化过了，跳过重复初始化")
		return
		
	print("PlayerStats initialize() 开始...")
	
	# 设置统计面板尺寸 - 不覆盖场景中的位置设置，增加高度以容纳标题
	size = Vector2(PANEL_WIDTH, PANEL_HEIGHT + 12)
	
	# 只在没有设置位置时才设置默认位置
	if position == Vector2.ZERO:
		anchors_preset = Control.PRESET_TOP_LEFT
		position = Vector2(10, 35)  # 在血条下方
	
	create_stats_panel()
	create_ui_elements()
	update_display()
	
	is_initialized = true
	print("PlayerStats 初始化完成，尺寸: ", size, " 位置: ", position)

func create_stats_panel():
	# 创建统计面板背景
	stats_panel = Panel.new()
	add_child(stats_panel)
	stats_panel.size = size
	stats_panel.add_theme_color_override("bg_color", Color(0.1, 0.1, 0.1, 0.7))
	
	# 添加标题
	var title_label = Label.new()
	stats_panel.add_child(title_label)
	title_label.text = "玩家状态"
	title_label.position = Vector2(5, -12)
	title_label.size = Vector2(PANEL_WIDTH - 10, 10)
	title_label.add_theme_color_override("font_color", Color.CYAN)
	title_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	title_label.add_theme_constant_override("shadow_offset_x", 1)
	title_label.add_theme_constant_override("shadow_offset_y", 1)
	title_label.add_theme_font_size_override("font_size", 8)
	
	# 创建抗锯齿字体
	var title_font = SystemFont.new()
	title_font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	title_font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	title_font.hinting = TextServer.HINTING_LIGHT
	title_label.add_theme_font_override("font", title_font)

# 创建抗锯齿字体的辅助函数
func create_clear_font() -> SystemFont:
	var font = SystemFont.new()
	font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	font.hinting = TextServer.HINTING_LIGHT
	return font

func create_ui_elements():
	# 创建等级标签 - 调整尺寸和位置
	level_label = Label.new()
	stats_panel.add_child(level_label)
	level_label.position = Vector2(5, 2)
	level_label.size = Vector2(60, 15)
	level_label.add_theme_color_override("font_color", Color.YELLOW)
	level_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	level_label.add_theme_constant_override("shadow_offset_x", 1)
	level_label.add_theme_constant_override("shadow_offset_y", 1)
	level_label.add_theme_font_size_override("font_size", 10)
	level_label.add_theme_font_override("font", create_clear_font())
	
	# 创建经验值标签 - 调整尺寸和位置
	exp_label = Label.new()
	stats_panel.add_child(exp_label)
	exp_label.position = Vector2(5, 17)
	exp_label.size = Vector2(140, 12)
	exp_label.add_theme_color_override("font_color", Color.CYAN)
	exp_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	exp_label.add_theme_constant_override("shadow_offset_x", 1)
	exp_label.add_theme_constant_override("shadow_offset_y", 1)
	exp_label.add_theme_font_size_override("font_size", 9)
	exp_label.add_theme_font_override("font", create_clear_font())
	
	# 创建经验值进度条 - 调整尺寸
	exp_bar = ProgressBar.new()
	stats_panel.add_child(exp_bar)
	exp_bar.position = Vector2(5, 25)
	exp_bar.size = Vector2(120, 5)
	exp_bar.min_value = 0
	exp_bar.max_value = 100
	exp_bar.value = 0
	
	# 创建金币图标 - 调整尺寸
	coins_icon = TextureRect.new()
	stats_panel.add_child(coins_icon)
	coins_icon.texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/props_itens/bag_coins.png")
	coins_icon.position = Vector2(5, 33)
	coins_icon.size = Vector2(10, 10)
	coins_icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
	
	# 创建金币标签 - 调整尺寸和位置
	coins_label = Label.new()
	stats_panel.add_child(coins_label)
	coins_label.position = Vector2(18, 31)
	coins_label.size = Vector2(80, 15)
	coins_label.add_theme_color_override("font_color", Color.GOLD)
	coins_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	coins_label.add_theme_constant_override("shadow_offset_x", 1)
	coins_label.add_theme_constant_override("shadow_offset_y", 1)
	coins_label.add_theme_font_size_override("font_size", 10)
	coins_label.add_theme_font_override("font", create_clear_font())

func update_stats(level: int, experience: int, coins: int):
	player_level = level
	current_experience = experience
	player_coins = coins
	update_display()

func update_display():
	if not level_label or not exp_label or not exp_bar or not coins_label:
		return
	
	# 更新等级显示
	level_label.text = "Lv." + str(player_level)
	
	# 更新经验值显示 - 简化文本
	exp_label.text = "XP: " + str(current_experience) + "/" + str(experience_to_next_level)
	
	# 更新经验值进度条
	var exp_percentage = float(current_experience) / float(experience_to_next_level) * 100
	exp_bar.value = exp_percentage
	
	# 设置经验条颜色
	if exp_percentage > 80:
		exp_bar.modulate = Color.GREEN
	elif exp_percentage > 50:
		exp_bar.modulate = Color.YELLOW
	else:
		exp_bar.modulate = Color.CYAN
	
	# 更新金币显示
	coins_label.text = str(player_coins)

func add_experience(amount: int):
	current_experience += amount
	
	# 检查是否升级
	while current_experience >= experience_to_next_level:
		level_up()
	
	update_display()
	
	# 经验获得动画
	animate_experience_gain()

func level_up():
	current_experience -= experience_to_next_level
	player_level += 1
	experience_to_next_level = calculate_next_level_experience()
	
	# 升级动画
	animate_level_up()

func calculate_next_level_experience() -> int:
	# 简单的经验值计算公式
	return 100 + (player_level - 1) * 50

func add_coins(amount: int):
	player_coins += amount
	update_display()
	
	# 金币获得动画
	animate_coin_gain()

func animate_experience_gain():
	if exp_bar:
		var tween = create_tween()
		tween.tween_property(exp_bar, "modulate", Color.WHITE, 0.2)
		tween.tween_property(exp_bar, "modulate", Color.CYAN, 0.2)

func animate_level_up():
	if level_label:
		var tween = create_tween()
		tween.tween_property(level_label, "scale", Vector2(1.2, 1.2), 0.3)
		tween.tween_property(level_label, "scale", Vector2(1.0, 1.0), 0.3)
		tween.tween_property(level_label, "modulate", Color.GOLD, 0.2)
		tween.tween_property(level_label, "modulate", Color.YELLOW, 0.2)

func animate_coin_gain():
	if coins_label:
		var tween = create_tween()
		tween.tween_property(coins_label, "scale", Vector2(1.1, 1.1), 0.2)
		tween.tween_property(coins_label, "scale", Vector2(1.0, 1.0), 0.2) 